package com.huluobo.kotlinim1907a.wiget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.huluobo.kotlinim1907a.R
import com.hyphenate.chat.EMConversation
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMTextMessageBody
import com.hyphenate.util.DateUtils
import kotlinx.android.synthetic.main.view_conversation_item.view.*
import java.util.*

/**
 * Created by lc on 2022/2/8.
 */
class ConversationListItemView(context: Context, attr: AttributeSet? = null) :
    RelativeLayout(context, attr) {
    init {
        View.inflate(context, R.layout.view_conversation_item, this)
    }

    fun bindView(emConversation: EMConversation) {
        //用户名
        userName.text = emConversation.conversationId()
        //最后一条消息的内容
        if (emConversation.lastMessage.type == EMMessage.Type.TXT) {
            val body = emConversation.lastMessage.body as EMTextMessageBody
            lastMessage.text = body.message
        } else lastMessage.text = context.getText(R.string.no_text_message)//[图片][文件][表情][视频消息]
        //最后一条消息的时间戳
        val timestampString = DateUtils.getTimestampString(Date(emConversation.lastMessage.msgTime))
        timestamp.text = timestampString
        //未读消息的数量
        if (emConversation.unreadMsgCount > 0) {
            unreadCount.visibility = View.VISIBLE
            unreadCount.text = emConversation.unreadMsgCount.toString()
        } else unreadCount.visibility = View.GONE
    }
}